<template>
  <el-row>
    <el-card>
      <el-row>
        <el-button-group>
          <el-button :type="null === searchForm.auditRes ? 'primary' : ''" @click="changeStatus(null)">{{ tableText['orderList'] }}</el-button>
          <el-button v-for="item in tableText['results']" :key="item['value']" :type="item['value'] === searchForm.auditRes ? 'primary' : ''" @click="changeStatus(item['value'])">{{ item['desc'] }}</el-button>
        </el-button-group>
      </el-row>
      <el-divider />
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="tableText['createTime']">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['productType']">
          <el-select v-model="searchForm.loanProductType" :placeholder="$t('administrator.auditedList.prompt.productType')" style="width: 250px">
            <el-option
              v-for="item in tableText['productTypes']"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['reviewTime']">
          <el-date-picker
            v-model="reviewTime"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['auditResult']">
          <el-select v-model="searchForm.operationResult" clearable :placeholder="$t('administrator.auditedList.prompt.productType')">
            <el-option
              v-for="item in tableText['resultsAll']"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['stage']">
          <el-select v-model="searchForm.stageId" :placeholder="$t('administrator.auditedList.prompt.stage')">
            <el-option
              v-for="item in statusData"
              :key="item.id"
              :label="item.flowName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['orderNo']">
          <el-input v-model="searchForm.creditOrderNo" clearable :placeholder="$t('administrator.auditedList.prompt.orderNo')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="tableText['phoneNumber']">
          <el-input v-model="searchForm.userPhone" clearable :placeholder="$t('administrator.auditedList.prompt.phoneNumber')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.channelSource')">
          <el-select v-model="searchForm.channel" clearable :placeholder="$t('usrModule.list.prompt.channelSource')" @change="setSource">
            <el-option
              v-for="item in $t('usrModule.list.source')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.promotionMethod')">
          <el-select v-model="searchForm.promotionTypeStr" clearable multiple collapse-tags :placeholder="$t('usrModule.list.prompt.promotionMethod')">
            <el-option
              v-for="item in promotionData"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('usrModule.list.table.referrer')">
          <el-input v-model="searchForm.referrerName" clearable :placeholder="$t('usrModule.list.prompt.referrer')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="creditOrderNo"
          :label="tableText['orderNo']"
          min-width="180"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="auth(scope.row)">
              {{ scope.row.creditOrderNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          :label="tableText['createTime']"
          min-width="170"
        />
        <el-table-column
          prop="userPhone"
          :label="tableText['phoneNumber']"
          min-width="150"
        />
        <el-table-column
          prop="userGrade"
          :label="tableText['userLevel']"
          min-width="120"
        />
        <el-table-column
          prop="channelStr"
          :label="tableText['channelSource']"
          min-width="120"
        />
        <el-table-column
          prop="promotionTypeStr"
          :label="tableText['promotionMethod']"
          min-width="120"
        />
        <el-table-column
          prop="referrerName"
          :label="tableText['referrer']"
          min-width="120"
        />
        <el-table-column
          prop="loanProductType"
          :label="tableText['productType']"
          min-width="200"
        >
          <template slot-scope="scope">
            {{ setType(scope.row['loanProductType']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="applyMonth"
          :label="tableText['periodsNumber']"
          min-width="120"
        />
        <el-table-column
          prop="applyMoney"
          :label="tableText['loanAmount']"
          min-width="120"
        />
        <el-table-column
          prop="loanDays"
          :label="tableText['loanPeriod']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ (scope.row['loanDays'] && scope.row['applyMonth']) ? (scope.row['loanDays'] * scope.row['applyMonth']) : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="stageName"
          :label="tableText['stage']"
          min-width="120"
        />
        <el-table-column
          prop="currentStageStaffName"
          :label="tableText['letterReviewer']"
          min-width="120"
        />
        <el-table-column
          prop="historyAuditTime"
          :label="tableText['letterReviewTime']"
          min-width="170"
        />
        <el-table-column
          prop="operationResult"
          :label="tableText['auditResults']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ setResult(scope.row['operationResult']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="operationRemark"
          :label="tableText['letterOpinion']"
          min-width="170"
        />
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { formatDate, ObjToQueryString } from '@/utils/method'
import { allPackage } from '@/api/user'
import { listAuditStage, getGroupAuditList } from '@/api/risk'
export default {
  name: 'AuditedList',
  data() {
    return {
      loading: true,
      tableText: {},
      totalNumber: 0,
      time: null,
      reviewTime: null,
      statusData: [],
      tableData: [],
      promotionData: [],
      allPackageData: [],
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('administrator.auditedList')
    this.getAllPackage()
    this.getStageData()
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        auditRes: null,
        createTimeStart: null,
        createTimeEnd: null,
        loanProductType: '',
        historyAuditTimeStart: null,
        historyAuditTimeEnd: null,
        stageId: '',
        operationResult: null,
        creditOrderNo: null,
        userPhone: null,
        channel: null,
        promotionTypeStr: null,
        referrerName: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['createTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['createTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['createTimeStart'] = null
        data['createTimeEnd'] = null
      }
      if (this.reviewTime && this.reviewTime.length > 1) {
        data['historyAuditTimeStart'] = formatDate(new Date(this.reviewTime[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['historyAuditTimeEnd'] = formatDate(new Date(this.reviewTime[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['historyAuditTimeStart'] = null
        data['historyAuditTimeEnd'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      getGroupAuditList(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setType(data) {
      const name = this.tableText['productTypes'].filter(item => {
        return item.value.toString() === data.toString()
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    setResult(data) {
      const name = this.tableText['currentStageRes'].filter(item => {
        return item.value === data
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    changeStatus(status) {
      this.searchForm.auditRes = status
      this.searchForm.currentPage = 1
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getStageData() {
      listAuditStage()
        .then(res => {
          if (res.success) {
            if (res.data && res.data.length > 0) {
              this.statusData = res.data
              const obj = [{
                flowName: this.$t('common.all'),
                id: ''
              }]
              this.statusData = obj.concat(this.statusData)
            } else {
              this.statusData = this.tableText['status']
            }
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getAllPackage() {
      allPackage()
        .then(res => {
          if (res.success) {
            this.allPackageData = res.data
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    setSource() {
      const channel = this.searchForm.channel
      if (channel === 1) {
        this.promotionData = this.$t('usrModule.list.method')
      } else if (channel === 2) {
        this.promotionData = this.$t('usrModule.list.method1')
      } else if (channel === 3) {
        this.promotionData = []
        const arr = this.allPackageData.filter(v => {
          return v['mediaSource'] !== null
        })
        const node = []
        arr.forEach(v => {
          node.push({
            desc: v['mediaSource'],
            value: v['mediaSource']
          })
        })
        this.promotionData = node
      } else {
        this.promotionData = []
      }
      this.searchForm.promotionTypeStr = null
    },
    auth(item) {
      const paramData = {
        from: 'auditedList',
        status: item['status'],
        userId: item['userId'],
        orderId: item['id'],
        orderNo: item['creditOrderNo']
      }
      const para = ObjToQueryString(paramData)
      window.open(`${window.location.origin + window.location.pathname}#/usr/auth?${para}`, '_blank')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
.Top{
  margin-top: 10px;
}
</style>
